//
//  ViewController.m
//  LeesonPhotoBrowser
//
//  Created by leeson on 2018/2/8.
//  Copyright © 2018年 李斯芃 ---> 512523045@qq.com. All rights reserved.
//

#import "ViewController.h"
#import "LBModel.h"
#import "Masonry.h"
#import "RecipientInfoView.h"
#import "LBPhotoBrowserManager.h"

#define WEAKOBJECT(object) __weak __typeof(&*object) weakObject = object;

@interface ViewController ()<UIScrollViewDelegate>

@property (nonatomic, strong) RecipientInfoView *scrollView;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    NSArray *urlStr = @[
                               //大图
                               @"http://p3.pstatp.com/large/w960/53220001268b1a373be9",
                               @"http://p3.pstatp.com/large/w960/4ecc000654544a1e96de",
                               @"http://p7.pstatp.com/large/w960/4ecc000654557eb903f0",
                               @"http://p2.pstatp.com/large/w960/4ecc000654567d246735",
                               @"http://p7.pstatp.com/large/w960/53210001381963c04923",
                               @"http://p3.pstatp.com/large/w960/532100012cafac577bf6",
                               @"http://p3.pstatp.com/large/w960/53240001202edd4de4ba",
                               @"http://p7.pstatp.com/large/w960/4ecc00064914d964561f",
                               @"http://p7.pstatp.com/large/w960/4ecb000657d013fef1b0",
                               @"http://p7.pstatp.com/large/w960/4ecb000657d18a0ba76e",
                               @"http://p7.pstatp.com/large/w960/53200002f39564a44388",
                               @"http://p7.pstatp.com/large/w960/4ecb000657d251ea4188",
                               @"http://p7.pstatp.com/large/w960/4ecb0005eb3ad2b76742",
                               @"http://p7.pstatp.com/large/w960/53210000cb4647a71763"
                               ];
    
    /*
    NSMutableArray *items = [[NSMutableArray alloc]init];
    for (int i = 0; i < urlStr.count; i++) {
        LBURLModel *model = [LBURLModel new];
        model.thumbnailURLString = urlStr[i];
        model.largeURLString = urlStr[i];
        [items addObject:model];
    }
    
    LBModel *model = [LBModel new];
    for (int i = 0; i < urlStr.count ; i++) {
        LBURLModel *urlModel = items[i];
        LBURLModel *newModel = [[LBURLModel alloc]init];
        newModel.thumbnailURLString = urlModel.thumbnailURLString;
        newModel.largeURLString = urlModel.largeURLString;
        [model.urls addObject:newModel];
    }
    //[model loadFrames];
    [self.models addObject:model];
    */
    
    LBModel *models = [LBModel new];
    for (int i = 0; i < urlStr.count; i++) {
        LBURLModel *model = [LBURLModel new];
        model.thumbnailURLString = urlStr[i];
        model.largeURLString = urlStr[i];
        [models.urls addObject:model];
    }
    //[model loadFrames];
    [self.models addObject:models];
    
    //滚动视图
    self.scrollView = [[NSBundle mainBundle] loadNibNamed:@"RecipientInfoView" owner:self options:0][0];
    self.scrollView.delegate = self;
    [self.view addSubview:self.scrollView];
    [self.scrollView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.bottom.equalTo(@0);
    }];
    self.scrollView.infoModel = self.models[0];
    
    WEAKOBJECT(self.scrollView)
    [self.scrollView setCallBack:^(LBModel *cellModel, NSInteger tag) {
        NSMutableArray *items = [[NSMutableArray alloc]init];
        for (int i = 0 ; i < cellModel.urls.count; i++) {
            LBURLModel *urlModel = cellModel.urls[i];
            //UIImageView *imageView = weakObject.imageViews[i];
            LBPhotoWebItem *item = [[LBPhotoWebItem alloc] initWithURLString:urlModel.largeURLString frame:[cellModel.frames[i] CGRectValue]];//imageView.frame
            [items addObject:item];
        }
        [LBPhotoBrowserManager.defaultManager showImageWithWebItems:items selectedIndex:tag fromImageViewSuperView:weakObject].lowGifMemory = YES;
        
        [[[LBPhotoBrowserManager.defaultManager addLongPressShowTitles:@[@"保存",@"识别二维码",@"取消"]] addTitleClickCallbackBlock:^(UIImage *image, NSIndexPath *indexPath, NSString *title) {
            LBPhotoBrowserLog(@"%@,%ld",title,(long)indexPath.row);
        }]addPhotoBrowserWillDismissBlock:^{
            LBPhotoBrowserLog(@"即将销毁");
        }].needPreloading = NO;// 这里关掉预加载功能
        
        [[LBPhotoBrowserManager defaultManager] addPlaceholdImageCallBackBlock:^UIImage *(NSIndexPath *indexPath) {
            return [UIImage imageNamed:@"placehold.jpeg"];
        }];
        
    }];
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (NSMutableArray *)models{
    if (!_models) {
        _models = [NSMutableArray array];
    }
    return _models;
}


@end
